package com.lxy.demo.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ServletA",urlPatterns = "/ServletA")
public class ServletA extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String username = req.getParameter("username");
        req.getServletContext().setAttribute("username",username);
        System.out.println("A=" + username);
        //重定向方法
        resp.sendRedirect("/U2-day03/ServletB");
        //自定义状态码
        //resp.setStatus(304);
        //自定义响应头
        //resp.setHeader("Head","这是响应头");
        //自定义响应体
        //resp.getWriter().write("我是响应体");

        //获取请求方式
        System.out.println(req.getMethod());
        //获取对象名
        System.out.println(req.getContextPath());
        //获取请求头的里面的信息 key和值
        System.out.println(req.getHeader("Cookie"));
        //获取ip地址
        System.out.println(req.getRemoteAddr());

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
